import java.util.Arrays;
import java.util.Stack;

/**
 * Created with Intellij IDEA.
 * Description:
 * User: wangj
 * Date: 2022-03-26
 * Time: 10:16
 */
public class TestSort {
    //直接插入排序
    public static void insertSort(int[] array) {
        for (int i = 1; i < array.length; i++) {
            int tmp=array[i];
            int j = i-1;
            for (; j >=0 ; j--) {
                if(tmp<array[j]){
                    array[j+1]=array[j];

                }else {
                    break;
                }
            }
            array[j+1]=tmp;
        }
    }
    /**
     * 直接插入排序
     * @param array
     * @param gap
     */
    public static void shell(int[] array,int gap) {
        for (int i = gap; i < array.length; i++) {
            int tmp=array[i];
            int j = i-gap;
            for (; j >=0 ; j-=gap) {
                if(tmp<array[j]){
                    array[j+gap]=array[j];

                }else {
                    break;
                }
            }
            array[j+gap]=tmp;
        }
    }

    //希尔排序
    public static void shellSort(int[] array) {
        int gap = array.length;//10
        while (gap > 1) {
            gap /= 2;
            shell(array,gap);
        }
        shell(array,1);
    }
    //交换
    public static void swap(int[] array,int i,int j) {
        int tmp=array[i];
        array[i]=array[j];
        array[j]=tmp;
    }
    /**
     * 选择排序：
     *
     * @param array
     */

    public static void selectSort(int[] array) {
        for (int i = 0; i < array.length; i++) {
            for (int j = i+1; j < array.length; j++) {
                if(array[j]<array[i]){
                    swap(array,i,j);
                }
            }
        }
    }
    public static void selectSort2(int[] array) {
        for (int i = 0; i < array.length; i++) {
            int minIndex=i;
            for (int j = i+1; j < array.length; j++) {
                if(array[j]<array[minIndex]){
                    minIndex=j;
                }
            }
            swap(array,minIndex,i);
        }
    }
    //堆排序
    public static void heapSort(int[] array){

    }
    /**
     * 快速排序：
     *
     * @param array
     */
    public static void quickSort(int[] array) {
        quick(array,0,array.length-1);
    }

    private static void quick(int[] array,int start,int end) {
        if(start >= end) {
            return;
        }
        int pivot = partition(array,start,end);
        quick(array,start,pivot-1);
        quick(array,pivot+1,end);
    }

    /**
     * 一次划分函数
     * @param array
     * @param left
     * @param right
     * @return
     */
    private static int partition(int[] array,int left,int right) {
        int tmp=array[left];
        while(left<right){

            while (left<right&&tmp<=array[right]){
                right--;
            }
            array[left]=array[right];
            while (left<right&&tmp>=array[left]){
                left++;
            }
            array[right]=array[left];
        }
        array[left]=tmp;
        return left;
    }
    /**
     * 三数取中法
     * @param array
     * @param left
     * @param right
     * @return 三个数中的 中间数字的下标
     */
    private static int threeMid(int[] array,int left,int right) {
        int mid = (left+right) >>> 1;
        if(array[left]<array[right]){
            if(array[mid]<array[left]){
                return left;
            }
            if(array[mid]>array[right]){
                return right;
            }
        }else {
            if(array[mid]<array[right]){
                return right;
            }
            if(array[mid]>array[left]){
                return left;
            }
        }
        return mid;
    }
    /**
     * 快速排序：非递归实现
     * @param array
     */
    public static void quickSort2(int[] array) {
        Stack<Integer> stack = new Stack<>();
        int start = 0;
        int end = array.length - 1;
        int pivot = partition(array, start, end);
        if (pivot > start + 1) {
            stack.push(start);
            stack.push(pivot - 1);
        }
        if (pivot < end - 1) {
            stack.push(pivot + 1);
            stack.push(end);
        }
        while (!stack.empty()) {
            end = stack.pop();
            start = stack.pop();
            pivot = partition(array, start, end);
            if (pivot > start + 1) {
                stack.push(start);
                stack.push(pivot - 1);
            }
            if (pivot < end - 1) {
                stack.push(pivot + 1);
                stack.push(end);
            }
        }


    }
    public static void main(String[] args) {
        int [] arr={1,3,10,6,5,9,4,6};
        System.out.println("排序前: "+Arrays.toString(arr));
        long bfTime=System.currentTimeMillis();
        quickSort2(arr);
        long afTime=System.currentTimeMillis();
        System.out.println(afTime-bfTime);
        System.out.println("排序后: "+Arrays.toString(arr));

    }
}
